package com.tuniu.agents.common.command;

import com.tuniu.agents.command.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Default command implementation class
 * Stateful design, can store and modify state
 */
public class ConfigCommand implements Command {

    /**
     * Matcher
     */
    private CommandMatch match;

    /**
     * Action
     */
    private final CommandAction action;

    /**
     * Command configuration
     */
    private final CommandConfig commandConfig;

    /**
     * Constructor
     *
     * @param commandConfig Shortcut command configuration
     * @param match         Matcher
     * @param action        Action
     */
    public ConfigCommand(CommandConfig commandConfig, CommandMatch match, CommandAction action) {
        this.commandConfig = commandConfig;
        this.match = match;
        this.action = action;
    }

    @Override
    public String getId() {
        return this.commandConfig.getId();
    }

    @Override
    public String getCommandName() {
        return this.commandConfig.getCommandName();
    }

    @Override
    public int getPriority() {
        return this.commandConfig.getPriority();
    }

    @Override
    public String getDescription() {
        return this.commandConfig.getDescription();
    }

    @Override
    public Map<String, Object> getParams() {
        return this.commandConfig.getParams();
    }

    @Override
    public CommandLifecycle getLifecycle() {
        return this.commandConfig.getLifecycle();
    }

    @Override
    public void setParams(Map<String, Object> params) {
        Map<String, Object> currentParams = new HashMap<>(getParams());
        currentParams.putAll(params);
        this.commandConfig.setParams(currentParams);
    }

    @Override
    public CommandMatch getMatch() {
        return match;
    }

    public void setMatch(CommandMatch match) {
        this.match = match;
    }

    @Override
    public CommandAction getAction() {
        return action;
    }

    @Override
    public int compareTo(Command other) {
        System.out.println(this.getPriority() + " " + other.getPriority());
        return Integer.compare(other.getPriority(), this.getPriority());
    }
} 